YAHOO.namespace("card.paid");
YAHOO.namespace("card.paid.generate");

var Dom = YAHOO.util.Dom,
    Event = YAHOO.util.Event;

$(function () {

    if (!loading) {
        // Initialize the temporary Panel to display while waiting for external content to load
        var loading =
            new YAHOO.widget.Panel("loading",
                { width: "240px",
                    fixedcenter: true,
                    close: false,
                    draggable: false,
                    zindex: 4,
                    modal: true,
                    visible: false
                }
            );

        loading.setHeader("创建中, 请等待...");
        loading.setBody("<img src=\"http://l.yimg.com/a/i/us/per/gr/gp/rel_interstitial_loading.gif\"/>");
        loading.render(document.body);
    }

    var query_btn = new YAHOO.widget.Button("generate_btn", {label:"赠卡任务" });

    $('#generate_btn').click(function () {

        var cardValue = $('#cardParValue').val();
        var periodType = $('#cardValidityPeriodType').val();
        var period = $('#cardValidityPeriod').val();
        var cardType = $('#cardType').val();
        var description = $('#description').val();
        var usernames = $('#usernames').val();

        if(!cardValue || isNaN(cardValue)){
            alert("请输入星币值，必须为数字");
            return false;
        }

        if(!period || isNaN(period)){
            alert("请输入有效期限，必须为数字");
            return false;
        }


        if ($("#paidCardForm").validationEngine('validate')) {
            loading.show();
            $.post('/ozing/cards/present',
                {
                    cardType: cardType,
                    cardValue: cardValue,
                    periodType: periodType,
                    period: period,
                    description: description,
                    usernames: usernames
                },
                function (data) {
                    if(data.error){
                        loading.hide();
                        alert("用户不存在！");
                        return;
                    }
                    loading.hide();
                    alert("赠卡成功！");
                });
        }
    });


    $('#paidCardForm').validationEngine({
        promptPosition:'topRight',
        scroll: false
    });

});